package com.admin.control;

import java.io.IOException;
import java.net.URL;

import com.admin.common.Show;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;

public class adminViewControl {
    @FXML
    private Button btn_exit;
    @FXML
    private TreeView<String> treeView;
    @FXML
    private StackPane stackPane;
    @FXML
    private ImageView imageView;
    @FXML
    private Label usernameLabel;
    private String userName;

    public adminViewControl() {
    }

    public adminViewControl(String userName) {
        this.userName = userName;
    }

    @FXML
    public void initialize() {
        usernameLabel.setText(userName);
        TreeItem<String> rootItem = new TreeItem<>("Root");
        rootItem.setExpanded(true);
        TreeItem<String> child1 = new TreeItem<>("公众监督数据管理");
        TreeItem<String> child2 = new TreeItem<>("统计数据管理");
        TreeItem<String> grandChild11 = new TreeItem<>("公众监督数据列表");
        TreeItem<String> grandChild12 = new TreeItem<>("确认AQI数据列表");
        TreeItem<String> grandChild21 = new TreeItem<>("省分组检查统计");
        TreeItem<String> grandChild22 = new TreeItem<>("AQI指数分布统计");
        TreeItem<String> grandChild23 = new TreeItem<>("AQI指数趋势统计");
        TreeItem<String> grandChild24 = new TreeItem<>("其它数据统计");
        child1.getChildren().addAll(grandChild11, grandChild12);
        child2.getChildren().addAll(grandChild21, grandChild22, grandChild23, grandChild24);
        rootItem.getChildren().addAll(child1, child2);
        treeView.setRoot(rootItem);
        treeView.setShowRoot(false);
        expandAll(rootItem);
        treeView.getSelectionModel().selectedItemProperty().addListener((_, oldValue, newValue) -> {
            if (newValue != null) {
                handleNodeSelection(newValue);
            }
        });
        
    }

    /**
     * @param selectedItem 被选中的节点
     */
    private void handleNodeSelection(TreeItem<String> selectedItem) {
        String nodeValue = selectedItem.getValue();
        System.out.println("选中的节点: " + nodeValue);
        // 根据不同的节点值执行不同的操作
        switch (nodeValue) {
            case "公众监督数据列表":
                queryFeedbackList();
                break;
            case "确认AQI数据列表":
                queryAQIconfirm();
                break;
            case "省分组检查统计":
                queryProvince();
                break;
            case "AQI指数分布统计":
                querydistributetotalstatis();
                break;
            case "AQI指数趋势统计":
                querytrendtotalstatis();
                break;
            case "其它数据统计":
                queryOther();
                break;
            default:
                break;
        }
    }

    private void expandAll(TreeItem<String> item) {
        if (item != null && !item.isLeaf()) {
            item.setExpanded(true);
            for (TreeItem<String> child : item.getChildren()) {
                expandAll(child);
            }
        }
    }
    public void queryProvince() {
        try {
            Show.viewShow(stackPane, getClass().getResource("/com/admin/view/provinceView.fxml"));
            System.out.println("查询成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void queryFeedbackList() {
        try {
            URL url = getClass().getResource("/com/admin/view/feedbackListView.fxml");
            Show.viewShow(stackPane, url);
            System.out.println("反馈查询成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void queryFeedback() {
        try {
            URL url = getClass().getResource("/com/admin/view/feedbackView.fxml");
            Show.viewShow(stackPane, url);
            System.out.println("反馈查询成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void queryAQIconfirm() {
        try {
            Show.viewShow(stackPane, getClass().getResource("/com/admin/view/AQIconfirmView.fxml"));
            System.out.println("AQI确认查询成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void queryOther() {
        try {
            Show.viewShow(stackPane, getClass().getResource("/com/admin/view/otherView.fxml"));
            System.out.println("其他查询成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void querydistributetotalstatis() {
        try {
            Show.viewShow(stackPane, getClass().getResource("/com/admin/view/AQIdistributetotalstatis.fxml"));
            System.out.println("AQI统计查询成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void querytrendtotalstatis() {
        try {
            Show.viewShow(stackPane, getClass().getResource("/com/admin/view/AQItrendtotalstatis.fxml"));
            System.out.println("AQI趋势查询成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void exit() {
        try {
            Stage stage = (Stage) btn_exit.getScene().getWindow();
            URL url = getClass().getResource("/com/admin/view/IndexView.fxml");
            Show.show(stage, url, "登录页面");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
